

import isg.etsii.ull.es.*;
import isg.etsii.ull.es.DAO.PropiedadesDAO;
import isg.etsii.ull.es.DAO.UsuarioRegistradoDAO;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ServletPropiedad
 */
public class ServletPropiedad extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UsuarioRegistradoDAO URDAO = null;
	private PropiedadesDAO PDAO = null;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletPropiedad() {
        super();
        
        try {
			URDAO = new UsuarioRegistradoDAO();
			PDAO = new PropiedadesDAO();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
			dispatcher.forward(request,response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
        	try {
    			InsertarPropiedad(response, request);
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}		
	}
	
    protected void InsertarPropiedad (HttpServletResponse res, HttpServletRequest request)throws IOException, ClassNotFoundException, SQLException, ServletException {
    	String tipo = request.getParameter("tipo");
    	String direccion = request.getParameter("direccion");
    	
       if (tipo.equals("s1") ||  direccion.equals("s2")) {
		   HttpSession session = request.getSession();
		   
		   int error = 5;
		   
		   session.setAttribute("tipo", tipo);
		   
		   if (!tipo.equals("s1")) {
			   tipo = null;
		   }
		   
		   if (!direccion.equals("s2")) {
			   direccion = null;
		   }
		   
		   session.setAttribute("dir", direccion);
		   session.setAttribute("tipo", tipo);
		   session.setAttribute("error", error);
		   
		   RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/error.jsp");
	   	   dispatcher.forward(request,res);
	   }
	   else {
		    
		   Propiedad p = new Propiedad (); 
		   p.setDireccion(request.getParameter("direccion"));
		   
		   HttpSession session = request.getSession();
		   String user = session.getAttribute("username").toString();
		   
		   p.setDni(URDAO.devDNI(user));
		   
		   p.setPrecio(Float.parseFloat(request.getParameter("precio")));
		   p.setTipoProp(request.getParameter("tipo"));
		   
	       try {
		      PDAO.nuevo(p);
	       } catch (SQLException e) {
			   // TODO Auto-generated catch block
			 e.printStackTrace();
		   }
	       
	       session.setAttribute("p", p);
			
		   RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/MostrarPropiedad.jsp");
		   dispatcher.forward(request,res);
	   }
   }
}
